﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Highchart.Core.PlotOptions;
using System.Web.UI;
using Highchart.Core;

namespace Highchart.UI
{    
    public class ColumnChart : Chart, IChart
    {

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            RenderType = RenderType.column;
            Render();
        }

        public PlotOptionsColumn PlotOptions
        {
            get
            {
                object o = ViewState["PlotOptionsColumn"];
                if (o == null)
                    return new PlotOptionsColumn();
                return (PlotOptionsColumn)o;
            }
            set { ViewState["PlotOptionsColumn"] = value; }
        }

        public void Render()
        {

            script =
@"
var chart[@Id];
$(document).ready(function() {
    [@Localization]

    chart[@Id] = new Highcharts.Chart({
    chart: { renderTo: '[@Id]', defaultSeriesType: '[@RenderType]' },
    credits: { enabled: [@ShowCredits] },
    [@PlotOptions]
    [@Title]
    [@Subtitle]
    [@Legend]
    [@Exporting]
    [@XAxis]
    [@YAxis]
    [@ToolTip]
    [@Series]
	});

    [@JSONSource]

});";

            script = script.Replace("[@Localization]", Lang.ToString());

            script = script.Replace("[@JSONSource]", AjaxDataSource.ToString());
            script = script.Replace("[@PlotOptions]", PlotOptions.ToString());

            script = script.Replace("[@Id]", this.ClientID);
            script = script.Replace("[@RenderType]", RenderType.ToString());
            script = script.Replace("[@Legend]", Legend.ToString());
            script = script.Replace("[@Exporting]", Exporting.ToString());
            script = script.Replace("[@ShowCredits]", ShowCredits.ToString().ToLower());
            script = script.Replace("[@Title]", Title.ToString());
            script = script.Replace("[@Subtitle]", SubTitle.ToString());
            script = script.Replace("[@ToolTip]", Tooltip.ToString());
            script = script.Replace("[@YAxis]", YAxis.ToString());
            script = script.Replace("[@XAxis]", XAxis.ToString());
            script = script.Replace("[@Series]", Series.ToString());

            // handle special case for events, such as point click, mouseover etc
            // see PointEvents.cs for examples
            System.Text.RegularExpressions.Regex _reg = new System.Text.RegularExpressions.Regex(@"\""(function\(event\)\{.*?\})\""", System.Text.RegularExpressions.RegexOptions.Multiline);
            script = _reg.Replace(script, "$1");

            if (!string.IsNullOrEmpty(Theme))
            {

                var themeAPI = @"
var highchartsOptions = Highcharts.setOptions(themes['{0}']);";
                script = string.Concat(string.Format(themeAPI, Theme.ToString()), script);

            }

            System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "chart_" + ClientID, script, true);
                     
        }

    }
}
